package tags;

import groovy.lang.Closure;
import play.exceptions.TagInternalException;
import play.exceptions.TemplateExecutionException;
import play.mvc.Router;
import play.templates.*;
import play.templates.GroovyTemplate.ExecutableTemplate;
import java.util.*;
import java.io.PrintWriter;

import models.EEntity;
import models.Idea;
import models.Organization;
import models.Plan;
import models.Tag;
import models.Topic;

/**
 * MyFastTag.java Purpose: To Generate Fast Tags for reusable HTML code.
 * 
 * @version 1.0 5/16/2011
 * @author Ahmed Mahmoud
 */

@FastTags.Namespace("Fat7y.Tags")
public class MyFastTag extends play.templates.FastTags {

	/**
	 * Generates a html div element contains the rating UI for certain Idea.
	 * 
	 * @param args
	 *            tag attributes which can be idea, plan, topic, or all of them.
	 * @param body
	 *            tag inner body
	 * @param out
	 *            the output writer
	 * @param template
	 *            enclosing template
	 * @param fromLine
	 *            template line number where the tag is defined
	 */
	public static void _rating(Map<?, ?> args, Closure body, PrintWriter out,
			ExecutableTemplate template, int fromLine) {
		Object ideaObj = args.get("idea");
		Object planObj = args.get("plan");
		Object topicObj = args.get("topic");
		if ((ideaObj == null && planObj == null && topicObj == null)) {
			throw new TemplateExecutionException(template.template, fromLine,
					"Specify a template name", new TagInternalException(
							"Specify a rating and an id"));
		}
		out.println("<br>");
		try {
			if (ideaObj != null) {
				Idea idea = (Idea) ideaObj;
				out.println("<div id=\"idea-rating\">");
				out.println("<label id=\"idea-rating\">Rating: " + idea.rating
						+ "</label>");

				out.println("<div >");
				out.println("<input type=\"hidden\" id=\"ideaRating\" value=\'"
						+ idea.rating + "\'");
				out.println("name=\'" + idea.rating
						+ "\' /> <input type=\"hidden\" id=\"ideaId\"");
				out.println("value=\'" + idea.id + "\' name=\'" + idea.id
						+ "\' /> <input type=\"button\"");
				out.println("name=\"rate\" value=\"rate\" class=\"rate\" />");
				out.println("<ul class=\"rating\">");
				out.println("<li><a href=\"#\">This is just a piece of crap</a>");
				out.println("<li><a href=\"#\">Nothing too new or interesting</a></li>");
				out.println("<li><a href=\"#\">Not bad, I like it</a></li>");
				out.println("<li><a href=\"#\">I would like to see more of this</a></li>");
				out.println("<li><a href=\"#\">This is the best thing I've seen</a></li>");
				out.print("</li>\n</ul>\n</div>\n</div>\n");
			}
			if (topicObj != null) {
				Topic topic = (Topic) topicObj;
				out.println("<div id=\"topic-rating\">");
				out.println("<label>Rating: " + topic.topicRating + "</label>");
				out.println("<div >");
				out.println("<input type=\"hidden\" id=\"topicRating\" value=\'"
						+ topic.topicRating + "\'");
				out.println("name=\'" + topic.topicRating
						+ "\' /> <input type=\"hidden\" id=\"topicId\"");
				out.println("value=\'" + topic.id + "\' name=\'" + topic.id
						+ "\' /> <input type=\"button\"");
				out.println("name=\"rate\" value=\"rate\" class=\"rate\" >");
				out.println("<ul class=\"rating\">");
				out.println("<li><a href=\"#\">This is just a piece of crap</a>");
				out.println("<li><a href=\"#\">Nothing too new or interesting</a></li>");
				out.println("<li><a href=\"#\">Not bad, I like it</a></li>");
				out.println("<li><a href=\"#\">I would like to see more of this</a></li>");
				out.println("<li><a href=\"#\">This is the best thing I've seen</a></li>");
				out.print("</li>\n</ul>\n</div>\n</div>\n");
			}
			if (planObj != null) {
				Plan plan = (Plan) planObj;
				out.println("<div id=\"plan-rating\">");
				out.println("<label id=\"plan-rating\">Rating: "
						+ plan.planRating + "</label>");
				out.println("<div >");
				out.println("<input type=\"hidden\" id=\"planRating\" value=\'"
						+ plan.planRating + "\'");
				out.println("name=\'" + plan.planRating
						+ "\' /> <input type=\"hidden\" id=\"planId\"");
				out.println("value=\'" + plan.id + "\' name=\'" + plan.id
						+ "\' /> <input type=\"button\"");
				out.println("name=\"rate\" value=\"rate\" class=\"rate\" >");
				out.println("<ul class=\"rating\">");
				out.println("<li><a href=\"#\">This is just a piece of crap</a></li>");
				out.println("<li><a href=\"#\">Nothing too new or interesting</a></li>");
				out.println("<li><a href=\"#\">Not bad, I like it</a></li>");
				out.println("<li><a href=\"#\">I would like to see more of this</a></li>");
				out.println("<li><a href=\"#\">This is the best thing I've seen</a></li>");
				out.print("</ul>\n</div>\n</div>\n");
			}
			out.println("<br>");
		} catch (ClassCastException e) {
			throw new TemplateExecutionException(template.template, fromLine,
					"Specify a template name", new TagInternalException(
							"Specify a rating and an id"));
		}
	}

	/**
	 * Generates a Tag Cloud html elements.
	 * 
	 * @param args
	 *            tag attributes which is List of Tags to be displayed.
	 * @param body
	 *            tag inner body
	 * @param out
	 *            the output writer
	 * @param template
	 *            enclosing template
	 * @param fromLine
	 *            template line number where the tag is defined
	 */
	public static void _tagCloud(Map<?, ?> args, Closure body, PrintWriter out,
			ExecutableTemplate template, int fromLine) {
		out.println("<br>");
		out.println("<div id=\"TagCloud\">");

		out.println("<div id=\"tag-cloud-list\" onmousemove=\"stepping = (event.clientY - 250) / container.offsetHeight * 0.2 - 0.1;\">\n<ul>");

		List<Tag> tags = Tag.findAll();
		if (tags.size() == 0) {
			out.println("<li><a href=\"#\">No Tags for Now</a></li>");
		} else {
			Collections.shuffle(tags);
			tags = tags.subList(0, (tags.size() > 15 ? 15 : tags.size() - 1));
			for (Tag tag : tags) {
				out.println("<li><a href=\"\\TagsController\\getTaggedwith?tagId="
						+ tag.id + "\">" + tag.name + "</a></li>");
			}
		}
		out.println("</ul>\n</div>\n</div>");
		out.println("<br>");

	}

	/**
	 * Generates a html for displaying list of ideas.
	 * 
	 * @param args
	 *            tag attributes which is the list of ideas
	 * @param body
	 *            tag inner body
	 * @param out
	 *            the output writer
	 * @param template
	 *            enclosing template
	 * @param fromLine
	 *            template line number where the tag is defined
	 */
	public static void _displayIdeas(Map<?, ?> args, Closure body,
			PrintWriter out, ExecutableTemplate template, int fromLine) {

		Object ideasObj = args.get("ideas");
		List<Idea> ideas = (List<Idea>) ideasObj;
		String result = "<p class=\"items-list\">";
		result += "<span id=\"ideas\">Ideas</span>";
		if (ideas == null || ideas.size() == 0) {
			result += "<span class =\"title\"> No Results were found </span></br>";
		} else {
			int i = 0;
			for (Idea idea : ideas) {
				result += formatContent("Idea", idea.id, idea.title,
						idea.content, ++i);
			}

		}
		result += "</p>";
		out.println(result);
	}

	/**
	 * Generates a html for displaying list of topics.
	 * 
	 * @param args
	 *            tag attributes which is the list of topics
	 * @param body
	 *            tag inner body
	 * @param out
	 *            the output writer
	 * @param template
	 *            enclosing template
	 * @param fromLine
	 *            template line number where the tag is defined
	 */
	public static void _displayTopics(Map<?, ?> args, Closure body,
			PrintWriter out, ExecutableTemplate template, int fromLine) {

		Object topicsObj = args.get("topics");
		List<Topic> topics = (List<Topic>) topicsObj;
		String result = "<p class=\"items-list\">";
		result += "<span id=\"topics\">Topics</span>";
		if (topics == null || topics.size() == 0) {
			result = "<span class =\"title\"> No Results were found </span></br>";
		} else {
			int i = 0;
			for (Topic topic : topics) {
				result += formatContent("Topic", topic.id, topic.name,
						topic.description, ++i);
			}

		}
		result += "</p>";
		out.println(result);
	}

	/**
	 * Generates a html for displaying list of organizations.
	 * 
	 * @param args
	 *            tag attributes which is the list of organizations
	 * @param body
	 *            tag inner body
	 * @param out
	 *            the output writer
	 * @param template
	 *            enclosing template
	 * @param fromLine
	 *            template line number where the tag is defined
	 */
	public static void _displayOrganizations(Map<?, ?> args, Closure body,
			PrintWriter out, ExecutableTemplate template, int fromLine) {

		Object organizationsObj = args.get("organizations");
		List<Organization> organizations = (List<Organization>) organizationsObj;
		String result = "<p class=\"items-list\">";
		result += "<span id=\"organizations\">Organizations</span>";
		if (organizations == null || organizations.size() == 0) {
			result = "<span class =\"title\"> No Results were found </span></br>";
		} else {
			int i = 0;
			for (Organization organization : organizations) {
				result += formatContent("Organization", organization.id,
						organization.name, organization.description, ++i);
			}

		}
		result += "</p>";
		out.println(result);
	}

	/**
	 * Generates Html elements for the methods _displayTopics and _displayIdeas.
	 * 
	 * @param type
	 *            to specify if it's a "topic" or "idea".
	 * @param id
	 *            the id of the object to be viewed.
	 * @param title
	 *            the title of the object to be viewed
	 * @param content
	 *            the content of the object to be view.
	 * @param index
	 *            is the index of this object in the list to be viewed
	 * @return String, an HTML link for this object.
	 */
	public static String formatContent(String type, Long id, String title,
			String content, int index) {
		String urlType = type.toLowerCase();
		Map paramtersMap = new HashMap<String, Integer>();
		if (type.toLowerCase().equalsIgnoreCase("organization")) {
			urlType = "org";
		}
		paramtersMap.put(urlType + "ID", id);
		String url = Router.reverse("Browse.View" + type, paramtersMap).url;
		String result = "<a href=\""
				+ url
				+ "\">"
				+ "<span class=\"title\"> "
				+ index
				+ ") "
				+ title
				+ "</span>"
				+ "<span> "
				+ content.substring(0,
						(content.length() > 100 ? 100 : content.length()))
				+ " </span>" + "</a>";
		result += "<span class=\"separator\">ـــــــــــــــــــــــــــــــــــــــــــــــــــــ"
				+ "ـــــــــــــــــــــــــــــــــــــــــــــــــــــ"
				+ "ـــــــــــــــــــــــــــــــــــــــــــــــــــــ"
				+ "</span>";
		return result;
	}

}